package com.wms.controller;

import com.wms.bean.PageBean;
import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.entity.Material;
import com.wms.entity.Stock;
import com.wms.exception.V;
import com.wms.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "物料管理")
@Slf4j
@RequestMapping("/api")
public class MaterialController {
    @Autowired
    MaterialService materialService;
    @GetMapping("/material")
    @ApiOperation(value = "查询物料(包含条件查询)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页,默认第一页",dataTypeClass = Integer.class,example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数,默认10",dataTypeClass = Integer.class,example = "10"),
            @ApiImplicitParam(name = "type",value = "物料类别 1:产品/2:商品",required = true),
    })
    public ResultBean getMaterial(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize,Material material){
        PageBean pageBean=materialService.getMaterial(page,pageSize,material);
        return R.success("物料详情",pageBean);
    }

    @PostMapping("/material/add")
    @ApiOperation(value = "新增物料")
    public ResultBean addMaterial( @RequestBody Material material, BindingResult result){
        V.validate(result);
        int rs= materialService.addMaterial(material);
        return R.success("添加成功");
    }
    @PutMapping("/material/update")
    @ApiOperation(value = "修改物料")
    public ResultBean updateMaterial(@RequestBody Material material,BindingResult result){
        V.validate(result);
        int rs=materialService.updateMaterial(material);
        return rs==1?R.success("修改成功"):R.fail("修改失败");
    }

    @ApiOperation("删除物料")
    @DeleteMapping("/material/delete")
    @ApiImplicitParam(name = "id",value = "要删除的物料id",required = true)
    public ResultBean deleteMaterial(int id){
        int rs=materialService.deleteMaterial(id);
        return rs==1?R.success("删除成功"):R.fail("删除失败");
    }
    @ApiOperation("设置物料库存")
    @PostMapping("/material/setStock")
    public ResultBean setStock(@RequestBody Stock stock,BindingResult result){
        V.validate(result);
        int rs=materialService.setStock(stock);
        return rs==1?R.success("设置物料库存成功"):R.fail("设置物料库存失败");
    }

}
